package cn.bone.ou.data.service.impl;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.auth.JWTInfo;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.community.bean.CommunityMng;
import cn.bone.ou.community.mapper.CommunityMngMapper;
import cn.bone.ou.data.bean.CommunityFile;
import cn.bone.ou.data.mapper.CommunityFileMapper;
import cn.bone.ou.data.param.FileParam;
import cn.bone.ou.data.service.IFileService;
import cn.bone.ou.data.vo.FileVo;
import cn.bone.ou.vote.bean.VoteExtra;
import cn.bone.ou.vote.mapper.VoteExtraMapper;
import cn.bone.ou.vote.mapper.VoteInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class FileServiceImpl implements IFileService {

    @Resource
    private CommunityFileMapper fileMapper;
    @Resource
    private CommunityMngMapper mngMapper;
    @Resource
    private VoteExtraMapper voteExtraMapper;


    /**获取文件列表*/
    public CommonResult<DataGrid<FileVo.ListVo>> list(FileParam.ListParam param){
        List<CommunityFile> list = fileMapper.list(param.getCommunityId(),
                param.getStart(), param.getNum(),
                param.getName());
        int count = fileMapper.countList(param.getCommunityId(),param.getName());

        List<FileVo.ListVo> vos = new ArrayList<>();
        for(CommunityFile file : list){
            FileVo.ListVo vo = new FileVo.ListVo();
            vo.setFileId(file.getId());
            vo.setName(file.getName());
            vo.setSize(file.getSize());
            vo.setUrl(file.getUrl());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(file.getCreateTime()));
            CommunityMng mng = mngMapper.selectById(file.getCreateUserId());
            if(mng!=null){
                vo.setCreateUserName(mng.getRealName());
            }
            vos.add(vo);
        }
        DataGrid<FileVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**新增文件*/
    @MyLogger(msg = "新增文件")
    public CommonResult<Void> add(FileParam.AddParam param){
        CommunityFile file = new CommunityFile();
        file.setCommunityId(param.getCommunityId());
        file.setName(param.getName());
        file.setSize(param.getSize());
        file.setUrl(param.getUrl());
        JWTInfo jwtInfo = JWTInfo.get();
        file.setCreateUserId(jwtInfo.getUserId());
        file.setCreateTime(new Date());
        fileMapper.insert(file);
        return Ref.ok();
    }

    /**删除文件*/
    @MyLogger(msg = "删除文件")
    public CommonResult<Void> delete(FileParam.DeleteParam param){
        CommunityFile file = fileMapper.selectById(param.getFileId());
        if(file==null){
            return Ref.error("错误的文件编号");
        }

        //判断文件是否被引用
        VoteExtra extra = voteExtraMapper.hasFile(param.getFileId());
        if(extra!=null){
            return Ref.error("编号["+extra.getVoteId()+"]的投票引用了该文件,不能删除");
        }
        fileMapper.deleteById(param.getFileId());
        return Ref.ok();
    }
}
